package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.FirstCategoryCMD;
import com.glowdayz.glowmee.dao.FirstCategoryDAO;

public class FirstCategoryBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public FirstCategoryBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		request.setCharacterEncoding("utf-8");
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		String param_str = request.getParameter("param");
		String insertFirstCategoryText = request.getParameter("updateFirstCategoryText" + param_str);
		String updateSortKey_str = request.getParameter("updateSortKey" + param_str);
		String updateIsDisplay_str = request.getParameter("updateIsDisplay" + param_str);
		
		if (param_str != null && insertFirstCategoryText != null && updateSortKey_str != null && updateIsDisplay_str != null) {
			int param = Integer.parseInt(param_str);
			insertFirstCategoryText = Util.getInstance().incoding(insertFirstCategoryText);
			int updateSortKey = Integer.parseInt(updateSortKey_str);
			int updateIsDisplay = Integer.parseInt(updateIsDisplay_str);
			try {
				FirstCategoryCMD cmd = new FirstCategoryCMD(param, insertFirstCategoryText, updateSortKey, updateIsDisplay);
				FirstCategoryDAO.getInstance().updateFirstCategory(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		try {
			FirstCategoryDAO dao = FirstCategoryDAO.getInstance();
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("first", dao.getFirstCategoryList(page, 10, null));
			
			request.setAttribute("display", DBHandler.getInstance().getCount("firstcategory", "idFirstCategory", "isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("firstcategory", "idFirstCategory", "isDisplay='0'"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/firstCategory.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String insertFirstCategoryText = request.getParameter("insertFirstCategoryText");
		int insertSortKey = Integer.parseInt(request.getParameter("insertSortKey"));
		int insertIsDisplay = Integer.parseInt(request.getParameter("insertIsDisplay"));
		
		try {
			FirstCategoryCMD cmd = new FirstCategoryCMD(insertFirstCategoryText, insertSortKey, insertIsDisplay);
			FirstCategoryDAO.getInstance().insertFirstCategory(cmd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			FirstCategoryDAO dao = FirstCategoryDAO.getInstance();
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("first", dao.getFirstCategoryList(1, 10, null));
			
			request.setAttribute("display", DBHandler.getInstance().getCount("firstcategory", "idFirstCategory", "isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("firstcategory", "idFirstCategory", "isDisplay='0'"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/firstCategory.jsp");
		dpr.forward(request, response);
	}
	
}
